<template>
	<view class="page">
	  <view class="introduce">
	    <view class="imgBox"></view>
	    <view class="textBox">您正在向邮储车生活平台申请车秒贷服务，申请成功后可立即享受汽车融资服务及相关配套服务。</view>
	  </view>
	  <view class="prompt">
	    <view>温馨提示：本服务不收取任何款项，完全免费。您无需向任何人支付中介费等任何名义费用。申请过程中请务必确认您的申请推荐人（{{sellName}}）是{{enterpiseName}}员工，谨防上当受骗。</view>
	  </view>
	  <view class="btnBox">
	    <!-- <button open-type="getAuthorize" 
	      onGetAuthorize="onGetAuthorize" 
	      onError="onAuthError" 
	      scope='phoneNumber'
	      class="btn"
	      :disabled="isBtn"
	    >
	      立即申请
	    </button> -->
			<button open-type="getPhoneNumber"
			  class="btn"
			  :disabled="isBtn"
				@getphonenumber="decryptPhoneNumber"
			>
			  立即申请
			</button>
	  </view>
	</view>
</template>

<script>
	import {getAccessToken, getPhoneNumber} from '@/service/wxMin'
	let app = getApp();
	export default {
		data() {
			return {
				isBtn: false,
				sellName: '',
				enterpiseName: ''
			}
		},
		onLoad() {
			let gouchedaiData = app.globalData.gouchedaiData;
			this.sellName = gouchedaiData.sellName; 
			this.enterpiseName = gouchedaiData.enterpiseName;
		},
		methods: {
			decryptPhoneNumber(e) {
				this.isBtn = true;
				let grant_type = 'client_credential';
				let code = e.detail.code;
				let prams = {grant_type}
				this._getAccessToken(prams, code);
				
				// 测试默认手机号
				// let mobile = '13661203984';
				// let token = 'OTE5MDVmYjQ5ZDU4ZmRkZjljZDJhOTZlZTRlNTBiZjdiYjRkYWU5ZmE2ODZiNjQ5';
				// let userInfoData = {mobile, token};
				// uni.setStorage({
				// 	key: 'userInfo',
				// 	data: userInfoData,
				// 	success: function(res) {
				// 		uni.navigateTo({url: '../confirmation/confirmation'});
				// 	}
				// })
			},
			// -----------------------网络请求--------------------------
			_getAccessToken(prams, code) {
				let _this = this;
				getAccessToken(JSON.stringify(prams)).then(res => {
					_this._getPhoneNumber(code)
				}).catch(err => {
					_this.isBtn = false;
					uni.hideLoading();
				})
			},
			_getPhoneNumber(prams) {
				let _this = this;
				getPhoneNumber(prams).then(res => {
					let data = res.data;
					if(data.code == "0000") {
						let mobile = data.phone;
						let token = data.token;
						let userInfoData = {mobile, token};
						uni.setStorage({
							key: 'userInfo',
							data: userInfoData,
							success: function(res) {
								uni.navigateTo({url: '../confirmation/confirmation'});
							}
						})
					}
					_this.isBtn = false;
				}).catch(err => {
					_this.isBtn = false;
					uni.hideLoading();
				})
			}
		}
	}
</script>

<style>
	@import url("./index.css");
</style>
